<template>
  <div class="app-container">
    <el-form v-if="isShowTable === true" :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="请输入年度查询" prop="creditLabel">
        <el-input
          v-model="queryParams.duringYear"
          placeholder="请输入年度查询"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['student:mycredit:export']"
        >导出</el-button>
      </el-form-item>
    </el-form>

    <el-tag v-if="isShowTable === false" effect="dark" type="primary" size="small">您还没有对应的合同信息,无需申请展期</el-tag>
    <el-table v-if="isShowTable === true" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column width="55" type="selection" align="center" />
      <el-table-column width="120" label="年度" align="center" prop="duringYear" fixed="left" />
        <el-table-column width="120" label="借款年份" align="center" prop="contract.borrowYear" />
        <el-table-column width="120" label="账户名称" align="center" prop="contract.accountName" />
        <el-table-column width="120" label="借款有限时间内" align="center" prop="contract.borrowLimitYear" />
      <el-table-column width="180" label="贷款开始时间" align="center" prop="birth" >
          <el-table-column width="60" label="年" align="center" prop="contract.didYear" />
          <el-table-column width="60" label="月" align="center" prop="contract.didMonth" />
          <el-table-column width="60" label="日" align="center" prop="contract.didDay" />
      </el-table-column>
        <el-table-column width="120" label="贷款期限到后有限天数" align="center" prop="contract.countDay" />
        <el-table-column width="120" label="贷款期限到后不能超过几天" align="center" prop="contract.expireDay" />
        <el-table-column width="120" label="出生日期" align="center" prop="birth" />
        <el-table-column width="180" label="学生身份证" align="center" prop="cardIdentity" />
        <el-table-column width="120" label="户口性质" align="center" prop="residence" />
        <el-table-column width="120" label="毕业中学" align="center" prop="medium" :show-overflow-tooltip="true" />
        <el-table-column width="120" label="户籍" align="center" prop="household" :show-overflow-tooltip="true" />
        <el-table-column width="200" label="通讯地址" align="center" prop="address" :show-overflow-tooltip="true" />
        <el-table-column width="120" label="邮编" align="center" prop="postcode" :show-overflow-tooltip="true" />
        <el-table-column width="120" label="申贷原因" align="center" prop="applyReason" :show-overflow-tooltip="true" />
        <el-table-column width="180" label="学生身份证有效起始日期" align="center" prop="studentEffectiveCardIdentity">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.studentEffectiveCardIdentity) }}</span>
          </template>
        </el-table-column>
        <el-table-column width="180" label="学生身份证有效结束日期" align="center" prop="studentInvalidCardIdentity" >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.studentInvalidCardIdentity) }}</span>
          </template>
        </el-table-column>
      <el-table-column label="操作" prop="contract.isExtended" align="center" fixed="right" width="160" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-popconfirm
            v-if="scope.row.contract.isExtended !== '已通过'"
            placement="left"
            @onConfirm="submitForm(scope.row)"
            @onCancel="cancel"
            confirmButtonText='是的'
            cancelButtonText='不用了'
            icon="el-icon-info"
            iconColor="red"
            title="是否申请展期？"
          >
            <el-button slot="reference" size="small" :disabled="scope.row.contract.isExtended === '申请展期中' ? true : false" :type="scope.row.contract.isExtended === '申请展期中' ? 'info' : 'primary'">{{ scope.row.contract.isExtended === '申请展期中' ? '展期在审核中' : '申请展期'  }}</el-button>
          </el-popconfirm>
          <el-button v-if="scope.row.contract.isExtended === '已通过'" type="success" size="small">已经通过</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-if="isShowTable === true"
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
  import { listData, getData, delData, exportData } from "@/api/county/credit";
  import { getUserProfile } from "@/api/system/user";
  import { updateData } from '../../../api/hsien/contract'
  import { getCredits } from '../../../api/county/credit'

  export default {
    data() {
      return {
        isShowTable: false,
        dialogVisible: false,
        user: '',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 学生贷款表格数据
        dataList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态数据学生贷款
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          creditName: undefined,
          userName: undefined,
          duringYear: undefined
        },
        // 表单参数
        form: {},
        // 合同表单参数
        contract: {},
        // 表单校验
        rules: {
        },
      };
    },
    created() {
      this.getList()
      this.getUser()
    },
    methods: {
      getUser() {
        getUserProfile().then(response => {
          this.user = response.data;
        });
      },
      /** 查询学生贷款数据列表 */
      getList() {
        this.loading = true;
        getCredits(this.$store.state.user.name).then(response => {
          console.log(response)
          this.dataList = response.data
          this.total = response.data.length
          this.loading = false
          for (let i in response.data) {
            if (response.data[i].contractId === null) {
              this.isShowTable = false
            } else {
              this.isShowTable = true
            }
          }
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          creditId: undefined,
          duringYear: undefined,
          qq: undefined,
          birth: undefined,
          cardIdentity: undefined,
          residence: undefined,
          medium: undefined, household: undefined,
          email: undefined, address: undefined, postcode: undefined,
          guarantor: undefined, guaPhone: undefined, relation: undefined,
          guaCardIdentity: undefined, heath: undefined,
          applyReason: undefined, signatureDate: undefined, countyUnit: undefined,
          isSeal: undefined, sealDate: undefined, studentEffectiveCardIdentity: undefined,
          studentInvalidCardIdentity: undefined, supportCenterProvince: undefined,
          supportCenterCity: undefined, supportCenterArea: undefined,
          householdOfProvince: undefined, householdOfCity: undefined,
          householdOfArea: undefined, householdOfCounty: undefined,
          householdOfDetail: undefined, homeOfProvince: undefined,
          homeOfCity: undefined, homeOfArea: undefined, homeOfCounty: undefined,
          homeOfDetail: undefined, isTheSameAsHousehold: undefined,
          homeOfPostcode: undefined, homeOfPhone: undefined, concatOfName: undefined,
          concatWorkUnit: undefined, concatOfPhone: undefined, concatOfCardIdentity: undefined,
          collegeOfName: undefined, collegeOfInstitute: undefined,
          collegeOfMajor: undefined, collegeOfEducation: undefined,
          collegeWithStart: undefined, studentNo: undefined, typeOfMajor: undefined,
          collegeOfEducationalSystem: undefined,
          collegeWithEnd: undefined, workUnit: undefined, workFeature: undefined,
          phoneOfUnit: undefined, postcodeOfUnit: undefined, addressOfUnit: undefined,
          changeForReason: undefined,
          identityPathOfFront: undefined,
          identityPathOfReverse: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加学生贷款数据";
        this.form.userName = this.queryParams.userName;
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.creditId)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const creditId = row.creditId || this.ids
        getData(creditId).then(response => {
          this.form = response.data;
        });
      },
      /** 提交按钮 */
      submitForm: function(row) {
        this.contract = row.contract
        this.contract.isExtended = '申请展期中'
        updateData(this.contract).then(res => {
          this.msgSuccess('已发起申请展期，等候审核!')
          this.getList();
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const creditIds = row.creditId || this.ids;
        this.$confirm('是否确认删除学生贷款编码为"' + creditIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delData(creditIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportData(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      },
    },
  };
</script>
